var _ = require('lodash');
var path = require("path");
var orm = require("orm");
var dao = require(path.join(process.cwd(),"dao/DAO"));


var Promise = require("bluebird");
var uniqid = require('uniqid');

function doCheckProcessParams(params) {
	return new Promise(function(resolve,reject) {
		var info = {};
				
		resolve(info);
	});
}

function doCreateProcess(info) {
	return new Promise(function(resolve,reject) {
		dao.create("ProcessModel",_.clone(info),function(err,neworder){
			if(err) return reject("创建检测记录失败");
			info.order = neworder;
			resolve(info);
		});
	});
}


/**
	获取时间函数 —— SHTL
**/

Date.prototype.Format = function(fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}




function doGetProcess(info) {
	return new Promise(function(resolve,reject) {
		dao.show("ProcessModel",info.yiqi_id,function(err,neworder){

			if(err) return reject("获取检测记录详情失败");
			if(!neworder) return reject("仪器ID不能存在");
			info.order = neworder;
			resolve(info);
		})
	});
}

/**
	function doUpdateProcess(info) {
	return new Promise(function(resolve,reject) {
		dao.update("ProcessModel",info.yiqi_id,_.clone(info),function(err,newProcess){
			if(err) return reject("更新失败");
			info.process = newProcess;
			resolve(info);
		});
		
	});
} 

**/

module.exports.createProcess = function(params,cb) {
	doCheckProcessParams(params)
	.then(doCreateProcess)
	.then(function(info) {
		cb(null,info.order);
	})
	.catch(function(err) {
		cb(err);
	});
}


module.exports.getAllProcess = function(params,cb){
	
	var conditions = {};
	if(!params.pagenum || params.pagenum <= 0) return cb("pagenum 参数错误");
	if(!params.pagesize || params.pagesize <= 0) return cb("pagesize 参数错误"); 
	conditions["columns"] = {};
	if(params.yiqi_id) {
		conditions["columns"]["yiqi_id"] = params.yiqi_id;
	}
	if(params.yiqi_name) {
		conditions["columns"]["yiqi_name"] = params.yiqi_name;
	}
	if(params.yiqi_zhuangtai) {
		conditions["columns"]["yiqi_zhuangtai"] = params.yiqi_zhuangtai;
	}
	if(params.user_id) {
		conditions["columns"]["user_id"] = params.user_id;
	}
	if(params.yiqi_xinghao) {
		conditions["columns"]["yiqi_xinghao"] = params.yiqi_xinghao;
	}
	if(params.yiqi_chuchangbianhao) {
		conditions["columns"]["yiqi_chuchangbianhao"] = params.yiqi_chuchangbianhao;
	}
	if(params.yiqi_songjiandanwei) {
		conditions["columns"]["yiqi_songjiandanwei"] = params.yiqi_songjiandanwei;
	}
	if(params.yiqi_jiaozhunzhengshubh) {
		conditions["columns"]["yiqi_jiaozhunzhengshubh"] = params.yiqi_jiaozhunzhengshubh;
	}
	if(params.yiqi_zhizaoshang) {
		conditions["columns"]["yiqi_zhizaoshang"] = params.yiqi_zhizaoshang;
	}
	if(params.yiqi_chuanganqileixing) {
		conditions["columns"]["yiqi_chuanganqileixing"] = params.yiqi_chuanganqileixing;
	}
	if(params.yiqi_time) {
		conditions["columns"]["yiqi_time"] = params.yiqi_time;
	}
	if(params.yiqi_time) {
		conditions["columns"]["yiqi_time"] = params.yiqi_time;
	}
	
	if(params.query) {
		conditions["columns"]["yiqi_name"] = orm.like("%" + params.query + "%");
	}
	
	if(params.value1) {
		conditions["columns"]["user_id"] = params.value1;
	}
	if(params.value2) {
		conditions["columns"]["yiqi_songjiandanwei"] = params.value2;
	}
	if(params.value3) {
		conditions["columns"]["yiqi_zhizaoshang"] = params.value3;
	}
	if(params.value4) {
		conditions["columns"]["yiqi_zhuangtai"] = params.value4;
	}
	
	
	conditions["columns"]["yiqi_del"] = '0';

	dao.countByConditions("ProcessModel",conditions,function(err,count){
		if(err) return cb(err);
		pagesize = params.pagesize;
		pagenum = params.pagenum;
		pageCount = Math.ceil(count / pagesize);
		offset = (pagenum - 1) * pagesize;
		if(offset >= count) {
			offset = count;
		}
		limit = pagesize;

		// 构建条件
		conditions["offset"] = offset;
		conditions["limit"] = limit;
		// conditions["only"] = 
		conditions["process"] = "-create_time";


		dao.list("ProcessModel",conditions,function(err,process){
			if(err) return cb(err);
			var resultDta = {};
			resultDta["total"] = count;
			resultDta["pagenum"] = pagenum;
			resultDta["process"] = _.map(process,function(process){
				return process;//_.omit(order,);
			});
			cb(err,resultDta);
		})
	});
}






module.exports.getProcessUser = function(params,cb){
	
	var conditions = {};
	if(!params.pagenum || params.pagenum <= 0) return cb("pagenum 参数错误");
	if(!params.pagesize || params.pagesize <= 0) return cb("pagesize 参数错误"); 
	conditions["columns"] = {};
	if(params.yiqi_id) {
		conditions["columns"]["yiqi_id"] = params.yiqi_id;
	}
	if(params.yiqi_name) {
		conditions["columns"]["yiqi_name"] = params.yiqi_name;
	}
	if(params.yiqi_zhuangtai) {
		conditions["columns"]["yiqi_zhuangtai"] = params.yiqi_zhuangtai;
	}
	if(params.user_id) {
		conditions["columns"]["user_id"] = params.user_id;
	}
	if(params.yiqi_xinghao) {
		conditions["columns"]["yiqi_xinghao"] = params.yiqi_xinghao;
	}
	if(params.yiqi_chuchangbianhao) {
		conditions["columns"]["yiqi_chuchangbianhao"] = params.yiqi_chuchangbianhao;
	}
	if(params.yiqi_songjiandanwei) {
		conditions["columns"]["yiqi_songjiandanwei"] = params.yiqi_songjiandanwei;
	}
	if(params.yiqi_jiaozhunzhengshubh) {
		conditions["columns"]["yiqi_jiaozhunzhengshubh"] = params.yiqi_jiaozhunzhengshubh;
	}
	if(params.yiqi_zhizaoshang) {
		conditions["columns"]["yiqi_zhizaoshang"] = params.yiqi_zhizaoshang;
	}
	if(params.yiqi_chuanganqileixing) {
		conditions["columns"]["yiqi_chuanganqileixing"] = params.yiqi_chuanganqileixing;
	}
	if(params.yiqi_time) {
		conditions["columns"]["yiqi_time"] = params.yiqi_time;
	}
	if(params.yiqi_time) {
		conditions["columns"]["yiqi_time"] = params.yiqi_time;
	}
	
	if(params.query) {
		conditions["columns"]["yiqi_name"] = orm.like("%" + params.query + "%");
	}
	
	if(params.value1) {
		conditions["columns"]["user_id"] = params.value1;
	}
	if(params.value2) {
		conditions["columns"]["yiqi_songjiandanwei"] = params.value2;
	}
	if(params.value3) {
		conditions["columns"]["yiqi_zhizaoshang"] = params.value3;
	}
	if(params.value4) {
		conditions["columns"]["yiqi_zhuangtai"] = params.value4;
	}
	
	conditions["columns"]["user_id"] = global.UserName;
	conditions["columns"]["yiqi_del"] = '0';

	dao.countByConditions("ProcessModel",conditions,function(err,count){
		if(err) return cb(err);
		pagesize = params.pagesize;
		pagenum = params.pagenum;
		pageCount = Math.ceil(count / pagesize);
		offset = (pagenum - 1) * pagesize;
		if(offset >= count) {
			offset = count;
		}
		limit = pagesize;

		// 构建条件
		conditions["offset"] = offset;
		conditions["limit"] = limit;
		// conditions["only"] = 
		conditions["process"] = "-create_time";


		dao.list("ProcessModel",conditions,function(err,process){
			if(err) return cb(err);
			var resultDta = {};
			resultDta["total"] = count;
			resultDta["pagenum"] = pagenum;
			resultDta["process"] = _.map(process,function(process){
				return process;//_.omit(order,);
			});
			cb(err,resultDta);
		})
	});
}









module.exports.getProcess = function(yiqiID,cb) {
	if(!yiqiID) return cb("仪器ID不能为空");
	
	doGetProcess({"yiqi_id":yiqiID})
	.then(function(info){
		cb(null,info.order);
	})
	.catch(function(err) {
		cb(err);
	});

}

/**
	module.exports.updateProcess = function(yiqiID,params,cb) {
	if(!yiqiID) return cb("仪器ID不能为空");
	params["yiqi_id"] = yiqiID;
	doCheckProcessParams(params)
	.then(doUpdateProcess)	
	.then(function(info){
		cb(null,info.process);
	})
	.catch(function(err) {
		cb(err);
	});
	
}
**/


/**
 * 更新状态
 * 
 * @param  {[type]}   cat_id  分类ID
 * @param  {[type]}   newName 新的名称
 * @param  {Function} cb      回调函数
 */
module.exports.updateProcess = function(yiqi_id,newZhuangtai,cb) {
	var yiqi_time =  (new Date()).Format("yyyy-MM-dd hh:mm:ss");
	dao.updateProcess("ProcessModel",yiqi_id,newZhuangtai,yiqi_time,function(err,newProcess) {
		if(err) return cb("更新失败");
		cb(null,newProcess);
	});
}





// 删除列表：实际就是更改删除状态为 1
module.exports.deleteProcess = function(yiqi_id,cb) {
	dao.update("ProcessModel",yiqi_id,{'yiqi_del':'1'},function(err,newProcess) {
		if(err) return cb("删除失败");
		cb(null,newProcess);
	});
}
